package com.ljkj.blockpay.network;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;


public class Network {

    /**
     * 标识网络状态
     *
     * @author moon
     */
    public enum NetworkMode {
        NO_AVAILABLE_NETWORK,
        NET_WORK_OK
    }

    /**
     * 是否是2G网
     */
    public static boolean is2G(int netType) {
        return netType == NetworkType.CMWAP
                || netType == NetworkType.CTWAP
                || netType == NetworkType.TYPE_2G;
    }

    /**
     * 是否连接wifi
     */
    public static boolean isWifi(int netType) {
        return netType == NetworkType.WIFI;
    }

    private static final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
    //获取Mobile网络下的cmwap、cmnet
    private static String net_apn = "start";

    /**
     * 根据NetType判断APN
     */
    private static String getNetApn(int type) {
        switch (type) {
            case NetworkType.WIFI:
            case NetworkType.WIRED:
                return "wifi";
            case NetworkType.TYPE_2G:
            case NetworkType.TYPE_3G:
                return "net";
            case NetworkType.CTWAP:
            case NetworkType.CMWAP:
                return "wap";
            case NetworkType.NONE:
            case NetworkType.UNKNOWN:
            default:
                return "";
        }
    }

    /**
     * 根据网络类型的int类型标识获取网络类型的名字
     */
    public static String getNetworkName(int netWorkType) {
        switch (netWorkType) {
            case NetworkType.WIFI:
            case NetworkType.WIRED:
                return "wifi";
            case NetworkType.TYPE_2G:
                return "GPRS";
            case NetworkType.TYPE_3G:
                return "3G";
            case NetworkType.CTWAP:
                return "ctwap";
            case NetworkType.CMWAP:
                return "Cmwap";
            case NetworkType.NONE:
            case NetworkType.UNKNOWN:
            default:
                return "";
        }
    }

    /**
     * 判断当前系统是否有可用网络
     * 用于立即判断
     */
    public static boolean isNetWorkAvailable(Context context) {
        NetworkInfo info = getSystemNetworkInfo(context);
        return info != null && info.isAvailable();
    }

    /**
     * 获得系统网络信息
     *
     * @param context
     * @return
     */
    public static NetworkInfo getSystemNetworkInfo(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        return ni;
    }

    /**
     * 网络类型
     */
    public final static class NetworkType {
        /**
         * 未知.
         */
        public final static int UNKNOWN = 0;
        /**
         * 无网络连接.
         */
        public final static int NONE = 1;
        /**
         * CMWAP.
         */
        public final static int CMWAP = 2;
        /**
         * CTWAP.
         */
        public final static int CTWAP = 3;
        /**
         * 2G.
         */
        public final static int TYPE_2G = 4;
        /**
         * 3G.
         */
        public final static int TYPE_3G = 5;
        /**
         * WIFI.
         */
        public final static int WIFI = 6;
        /**
         * 有线.
         */
        public final static int WIRED = 7;
    }
}
